package com.example.satu.quizcreator.quiz_creator;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;

import com.example.satu.quizcreator.quiz.Question;
import com.example.satu.quizcreator.R;
import com.example.satu.quizcreator.quiz_creator.interfaces.onAddEditQuestionButtonClick;
import com.github.rtyley.android.sherlock.roboguice.fragment.RoboSherlockFragment;

import roboguice.inject.InjectView;

/**
 * Created by Satu on 2014-09-11.
 */
public class EditQuestionFragment extends RoboSherlockFragment {

    @InjectView(R.id.editText_question_body)
    EditText questionBody;

    @InjectView(R.id.button_edit_question)
    ImageButton editQuestionButton;

    onAddEditQuestionButtonClick onAddEditQuestionButtonClick;
    Question question;

    long questionIndex;


    public EditQuestionFragment(Question question) {
        this.question = question;
        questionIndex = question.getId();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.question_editor_layout,null);
    }
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        questionBody.setText(question.getTitle());


        editQuestionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onAddEditQuestionButtonClick.onAddEditQuestionButton(true);
            }
        });
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if( !(getParentFragment() instanceof onAddEditQuestionButtonClick) ){
            throw new RuntimeException("Twoja activity nie implementuje onNewsitemClickListneer");
        }
        onAddEditQuestionButtonClick = (onAddEditQuestionButtonClick) getParentFragment();
    }

    public String getQuestionBody() {
        return questionBody.getText().toString();
    }

    public long getQuestionIndex() {
        return questionIndex;
    }

    public void setQuestionIndex(long questionIndex) {
        this.questionIndex = questionIndex;
    }
}
